package data;

import domain.Perfil;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;
import util.UIDGenerator;

public class JDBCPerfilDAO implements IPerfilDAO{
	public void deletePerfil(String perfilID){
		Connection conn = ConnectionManager.getInstance().checkOut();
		System.out.println("Conexion abierta");
		String sql = "DELETE FROM perfil WHERE (perfilOID = ?)";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, perfilID);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("Message: " + e.getMessage());
            System.err.println("SQLState: " + e.getSQLState());
            System.err.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("Conexion cerrada");
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }		
	}
	
	public void insertPerfil(String usuarioID, Perfil p){
		Connection conn = ConnectionManager.getInstance().checkOut();
		System.out.println("Conexion abierta");
		String sql = "INSERT INTO perfil(perfilOID, usuarioOID, titulacion, master, aniosExperiencia) VALUES(?, ?, ?, ?)";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, UIDGenerator.getInstance().getKey());
            stmt.setString(2, usuarioID);
            stmt.setString(3, p.getTitulacion());
            stmt.setString(4, p.getMaster());
            stmt.setInt(5, p.getAniosExperiencia());

            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("Conexion cerrada");
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
	}
	
	public Perfil selectPerfil(String perfilID){
		Connection conn = ConnectionManager.getInstance().checkOut();
		System.out.println("Conexion abierta");
		String sql = "SELECT * FROM perfil WHERE (perfilOID = ?)";
        PreparedStatement stmt = null;
        ResultSet result = null;
        Perfil p = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, perfilID);

            result = stmt.executeQuery();

            result.next();
            p = new Perfil();
            p.setPerfilID(result.getString("perfilOID"));
            p.setUsuarioID(result.getString("usuarioOID"));
            p.setTitulacion(result.getString("titulacion"));
            p.setMaster(result.getString("master"));
            p.setAniosExperiencia(result.getInt("aniosExperiencia"));
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("Conexion cerrada");
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return p;
    }

	public List<Perfil> selectAllPerfiles(){
		Connection conn = ConnectionManager.getInstance().checkOut();
		System.out.println("Conexion abierta");

        PreparedStatement stmt = null;
        List<Perfil> listaPerfiles = new LinkedList<Perfil>();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM perfil";
            stmt = conn.prepareStatement(sql);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
                Perfil p = new Perfil();
                p.setPerfilID(result.getString("perfilOID"));
                p.setUsuarioID(result.getString("usuarioOID"));
                p.setTitulacion(result.getString("titulacion"));
                p.setMaster(result.getString("master"));
                p.setAniosExperiencia(result.getInt("aniosExperiencia"));
                listaPerfiles.add(p);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("Conexion cerrada");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
        return listaPerfiles;
	}
}